package com.lazy.mongo.ex;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;
import org.junit.Test;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.DeleteOneModel;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateOneModel;

import static com.mongodb.client.model.Filters.*;

public class Demo1 {

    @Test
    public void insertOne() {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

        MongoDatabase database = mongoClient.getDatabase("mydb");        
        
        MongoCollection<Document> collection = database.getCollection("test");
        Document doc = new Document("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("info", new Document("x", 203).append("y", 102));
        
        collection.insertOne(doc);
    }
    
    @Test
    public void insertMany() {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

        MongoDatabase database = mongoClient.getDatabase("mydb");        
        
        MongoCollection<Document> collection = database.getCollection("test");
        
        List<Document> documents = new ArrayList<Document>();
        for (int i = 0; i < 100; i++) {
            Document doc = new Document("name", "MongoDB" + i)
                    .append("type", "database")
                    .append("count", 1)
                    .append("info", new Document("x", 203).append("y", 102));
            documents.add(doc);
        }

        collection.insertMany(documents);
    }
    
    @Test
    public void  findFirst() {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase database = mongoClient.getDatabase("mydb");        
        MongoCollection<Document> collection = database.getCollection("test");
        
        Document myDoc = collection.find().first();
        System.out.println(myDoc.toJson()); 
    }
    
    @Test
    public void  findAll() {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase database = mongoClient.getDatabase("mydb");        
        MongoCollection<Document> collection = database.getCollection("test");
        
        MongoCursor<Document> cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
    }
    
    @Test
    public void  findFilter() {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase database = mongoClient.getDatabase("mydb");        
        MongoCollection<Document> collection = database.getCollection("test");
        
        Document myDoc = collection.find(eq("name", "MongoDB71")).first();
        System.out.println(myDoc.toJson());
    }
    
    @Test
    public void bulkWrite() {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase database = mongoClient.getDatabase("mydb");        
        MongoCollection<Document> collection = database.getCollection("testbulk");
        
        // 2. Ordered bulk operation - order is guarenteed
        //collection.bulkWrite(Arrays.asList(new InsertOneModel<>(new Document("_id", 4)));
        collection.bulkWrite(
          Arrays.asList(new InsertOneModel<>(new Document("_id", 4)),
                        new InsertOneModel<>(new Document("_id", 5)),
                        new InsertOneModel<>(new Document("_id", 6))
                        //new UpdateOneModel<>(new Document("_id", 1),
                        //                     new Document("$set", new Document("x", 2))),
                        //new DeleteOneModel<>(new Document("_id", 2))
                        //new ReplaceOneModel<>(new Document("_id", 3),
                        //                      new Document("_id", 3).append("x", 4))
          ));


         // 2. Unordered bulk operation - no guarantee of order of operation
//        collection.bulkWrite(
//          Arrays.asList(new InsertOneModel<>(new Document("_id", 4)),
//                        new InsertOneModel<>(new Document("_id", 5)),
//                        new InsertOneModel<>(new Document("_id", 6)),
//                        new UpdateOneModel<>(new Document("_id", 1),
//                                             new Document("$set", new Document("x", 2))),
//                        new DeleteOneModel<>(new Document("_id", 2)),
//                        new ReplaceOneModel<>(new Document("_id", 3),
//                                              new Document("_id", 3).append("x", 4))),
//          new BulkWriteOptions().ordered(false));
    }
}
